$(document).ready(
				function() {
					var contextPath = document.location.pathname; 
					var index =contextPath.substr(1).indexOf("/"); 
					contextPath = contextPath.substr(1,index); 
				var basePath="/"+contextPath;
					var totalCount = $("#totalCount").val();
					var pSize=$("#pageSize").val();;
					var pageIndex = $("#pageIndex").val();
					var pageCount = $("#pageCount").val();
					if(pageCount==null ||pageCount==""||pageCount=="undefined")
						{
						pageCount=1
						}

					$("#sequenceDetail").on('hide.bs.modal', function() {
						
					});

					$("#optBtn")
							.delegate(
									'#saveBtn','click',
									function() {
										
										$("#protypeDetail").attr("method","post");
										
										var seqId = $("#seqId").val();
									
											if (seqId!=null && seqId ==""||seqId=="undefined") {
												
												 $("#sequenceDetail").attr("action","save.do");
											} else {
												
												 $("#sequenceDetail").attr("action","update.do");
											}
											$("#sequenceDetail").submit();
										
									});

					$('#pagination2')
							.jqPaginator(
									{
										 totalPages: pageCount,
										    visiblePages: pageCount,
										    currentPage: 1,
										
									/**	first: '<li class="first"><a href="javascript:void(0);">第一页</a></li>',**/
										    prev: '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
										  /**  last: '<li class="last"><a href="javascript:void(0);">Last</a></li>',**/
										    page: '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
										    next: '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
										onPageChange : function(num, type) {
//											alert("pageIndex="+num);
//											alert("type="+type);
											$("#pageIndex").val(num);
											var queryUrl = "queryByPage.do";
											$.ajax({
														url : queryUrl,
														dataType : 'json',
														data : {
															pageIndex :num,
															pageSize : pSize
														},
														cache : false,
														success : function(data) {

															
															$("#protypes tr:not(:first)").remove();
															var results = data.pageResult.resultList;
															
															for (i = 0; i < results.length; i++) {
																var seq = results[i];
															
																var rowContent = "<tr>"
																		+ "<td style='text-align:center'><input name='typeId' class='checkbox' type='checkbox' value='"
																		+ seq.id
																		+ "'/></td>"
																		+ "<td style='text-align:center'>"
																		+ seq.seqName
																		+ "</td>"
																		+ "<td style='text-align:center'>"
																		+ seq.tableName
																		+ "</td>"
																		+ "<td style='text-align:center'>"
																		+ seq.nums
																		+ "</td>"
																		+ "<td style='text-align:center'>"
																		+ seq.maxId
																		+ "</td>"
																		+ "<td style='text-align:center'>"
																		+ seq.prefix
																		+ "</td>"
																rowContent = rowContent
																+"<td style='text-align:center'>"+ seq.note
																		+ "</td>"
																		+ 
																		"<td style='text-align:center'><button name='editBtn' objId='"
																		+ seq.id
																		+ "' class='btn btn-sm-block'>修改</button></td>"
																		+ "</tr>";
																$("#protypes")
																		.append(rowContent);
															}

														},
														error : function(html) {
															return;
														}
													});
										}
									});

					/**
					 * 全选当前页记录
					 */
					$("#protypes").delegate('#selectAll', 'click', function() {
						var isSelectAll = $(this).prop('checked');
						$("input[name='typeId']").each(function() {
							$(this).prop("checked", isSelectAll);
						});
					});

					/**
					 * 修改按钮事件
					 */

					$("#protypes").delegate('[name="editBtn"]', 'click', function() {
						var typeId = $(this).attr("objId");
						
						var editUrl = "show.do";
						$.ajax({

							url : editUrl,
							dataType : 'json',
							data : {
								id : typeId
							},
							cache : false,

							success : function(data) {
								
								loadSequence(data);
							},
							error : function(html) {
								// alert(html);
								alert("Load Data failly");
								refresh();
							}
						});

					});
					
					$("#protypes").delegate('[name="showList"]', 'click', function() {
						var typeId = $(this).attr("objId");
						
						var editUrl =basePath+"/manage/proInfor/queryByProType.do";
						document.location.href=editUrl+"?typeId="+typeId;

					});
					
					

					$("#tools")
							.delegate(
									'#deleteBtn',
									'click',
									function() {
										delChk = $('input[name="typeId"]:checked');

										if (delChk.length > 0) {
											if (confirm("Are you sure delete the record?")) {

												var ids = "";
												/*var delUrl = "delete.do?ids=";*/
												var delUrl = "delete.do";
                                                var idArr=new Array();
                                               
                                                
                                                for (var i = 0; i < delChk.length; i++) {
                                                	idArr.push(delChk.get(i).value);
                                           
											}
												
												$.ajax({
															url : delUrl,
															type: "POST",
															data : {
																ids : idArr.toString()
															},
															cache : false,

															success : function(
																	html) {
																alert("Delete successfully");
																refresh();
															},
															error : function(
																	html) {
																alert("Delete failly");
																refresh();
															}
														});
											} else {
												return;
											}
										} else {
											alert("Please select the record to delete");
											return;
										}
									});

					$("#tools").delegate('#addUser', 'click', function() {

						$("#sequenceDialog input").each(function() {
							$(this).prop("value", null);
							$(this).prop("readonly", false);

						});

						$("#sequenceDialog textarea").each(function() {
							$(this).prop("value", null);

						});
						$("#sequenceDialog").modal('show');

					});
					
					
					

				});


$("#seqName").delegate($(this),'change',function(){
	var content=$(this).val();
	var target=$("#prefix");
	if(content!=null && content!="" && content!="undefined"){
		genPrefix(content,target);
	}else{
		alert("序列名不能为空");
		return;
	}
});


$("#seqName").delegate($(this),'blur',function(){
	var content=$(this).val();
	var target=$("#prefix");
	if(content!=null && content!="" && content!="undefined"){
		genPrefix(content,target);
	}else{
		alert("序列名不能为空");
		return;
	}
});


function genPrefix(content,target)
{
	var chkUrl =encodeURI("queryPerfixBySeqName.do?content='"+content+"'");
	
      $.ajax({
    		url : chkUrl,
    		dataType: 'text',
    		
			
			cache : false,

			success : function(data) {
				if(data!=null && data!=""){
					target.val(data);
				}
				
			},
			error : function(
					html) {
				alert("系统出错");
				refresh();
			}
    	  
      });
}


function loadSequence(seq) {
   
	
	$("#seqId").val(seq.id);
	$("#tableName").val(seq.tableName);
	$("#seqName").val(seq.seqName);
	$("#prefix").val(seq.prefix);
	
	if(seq.maxId==null||seq.maxId==""||seq.maxId=="undefined"){
			seq.maxId=0;
		
	}
	
	$("#maxId").val(seq.maxId);
	$("#nums").val(seq.nums);
	
	$("#note").val(seq.note);
	$("#sequenceDialog").modal('show');
}

function vaildate(form) {
	return true;
}

function refresh() {
	window.location.reload();
}
